package lesson02;

/**
 * 演示线程是否存活
 *
 * @author wileda
 * @date 2023/01/04  17:42
 */
public class Demo03 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            int count = 0;
            while(true) {
                System.out.println("hello thread" + count++);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (count >= 5) {
                    break;
                }
            }
        });
        //启动之前查看线程是否存活
        System.out.println("启动之前是否存活 " + thread.isAlive());
        //启动线程
        thread.start();
        //等待一秒，由于创建线程申请资源需要时间，确保线程创建完成开始执行
        Thread.sleep(1000);
        System.out.println("启动之后是否存活 " + thread.isAlive());
        //等待线程执行完成
        thread.join();
        System.out.println("线程执行完成之后是否存活 " + thread.isAlive());
    }
}
